package com.zookeeper.demo1;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.data.Stat;

public class CuratorTest {


    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("192.168.93.10:2181")
                .sessionTimeoutMs(60 * 1000)  // 会话超时时间
                .connectionTimeoutMs(5000) // 连接超时时间
                .retryPolicy(retryPolicy)
                .build();
        client.start();
        //创建节点
//        String s = client.create().forPath("/bbb", "456".getBytes());
//        System.out.println(s);

//        byte[] bytes = client.getData().forPath("/bbb");
//       System.out.println(new String(bytes));
//
//        Stat stat = client.checkExists().forPath("/bbb");
//        System.out.println(stat);

        Stat stat = client.setData().forPath("/bbb", "456".getBytes());
        System.out.println(stat);

//        client.delete().forPath("/bbb");

//        String path = client.create().forPath("/bbb/test", "xxx".getBytes());
//        System.out.println(path);

//        List<String> list = client.getChildren().forPath("/bbb");
//        System.out.println(list);

//        client.createContainers("/bbb-containers");

//        String path = client.create().withTtl(3000).withMode(CreateMode.PERSISTENT_WITH_TTL).forPath("/bbb-ttl");
//        System.out.println(path);


//        client.getData().usingWatcher(new CuratorWatcher() {
//            @Override
//            public void process(WatchedEvent watchedEvent) throws Exception {
//                System.out.println(watchedEvent);
//            }
//        }).forPath("/bbb");


        // NodeCache监听自身节点的数据变化
//        NodeCache nodeCache = new NodeCache(client, "/bbb");
//        nodeCache.start();
//        nodeCache.getListenable().addListener(new NodeCacheListener() {
//            @Override
//            public void nodeChanged() throws Exception {
//                System.out.println(123);
//            }
//        });


        // PathChildrenCache能够监听自身节点下的子节点的变化
//        PathChildrenCache pathChildrenCache = new PathChildrenCache(client, "/bbb", true);
//        pathChildrenCache.start();
//
//        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
//            @Override
//            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
//                switch (event.getType()) {
//                    case CHILD_ADDED: {
//                        System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
//                        break;
//                    }
//
//                    case CHILD_UPDATED: {
//                        System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
//                        break;
//                    }
//
//                    case CHILD_REMOVED: {
//                        System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
//                        break;
//                    }
//                }
//            }
//        });


        // TreeCache即能够监听自身节点的变化，也能监听子节点的变化
//        TreeCache treeCache = new TreeCache(client, "/bbb");
//        treeCache.start();
//        treeCache.getListenable().addListener(new TreeCacheListener() {
//            @Override
//            public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent event) throws Exception {
//                if (event.getData() != null) {
//                    System.out.println("type=" + event.getType() + " path=" + event.getData().getPath());
//                } else {
//                    System.out.println("type=" + event.getType());
//                }
//            }
//        });

//        System.in.read();


        // 一个监听器
        // 节点创建，子节点的创建
        // 节点内容的改变，子节点内容的改变
        // 节点的删除，子节点的删除
//        CuratorCacheListener listener = CuratorCacheListener.builder()
//                .forCreates(node -> System.out.println(String.format("Node created: [%s]", node)))
//                .forChanges((oldNode, node) -> System.out.println(String.format("Node changed. Old: [%s] New: [%s]", oldNode, node)))
//                .forDeletes(oldNode -> System.out.println(String.format("Node deleted. Old value: [%s]", oldNode)))
//                .forInitialized(new Runnable() {
//                    @Override
//                    public void run() {
//                        System.out.println("Cache initialized");
//                    }
//                })
//                .build();
//
//
//        CuratorCache curatorCache = CuratorCache.build(client, "/bbb");
//        curatorCache.listenable().addListener(CuratorCacheListener.builder().forAll(listener).build());
//        curatorCache.start();
//
//        System.in.read();


//        String s = client.create().withMode(CreateMode.PERSISTENT_SEQUENTIAL).inBackground(new BackgroundCallback() {
//            @Override
//            public void processResult(CuratorFramework curatorFramework, CuratorEvent curatorEvent) throws Exception {
//                System.out.println(curatorEvent);
//            }
//        }).forPath("/bbb/h_", "123".getBytes());
//
//        System.out.println(s);
//
//        System.in.read();



    }
}
